package org.deri.odhd.buses.index;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.deri.odhd.buses.model.Journey;

public class JourneyManager {

	Indexer indexer;
	Set<Journey> journeys;
	Map<String, Journey> journeysMap = new HashMap<String, Journey>(); 
	
	public JourneyManager() throws IOException{
		this.indexer = new Indexer();
	}
	
	public JourneyManager(Set<Journey> journeys) throws IOException {
		this.journeys = journeys;
		this.indexer = new Indexer();
		this.indexer.indexJourneys(journeys);
		memoryCrossIndex();
	}
	
	public List<Journey> find(String day, String from, String to) throws IOException{
		List<String> ids = indexer.search(day, from, to);
		List<Journey> journeys = new ArrayList<Journey>();
		for(String id:ids){
			Journey j = journeysMap.get(id);
			journeys.add(j);
		}
		return journeys;
	}
	
	public List<Journey> findJourneys(String day, String from, String to) throws IOException{
		return indexer.searchJourneys(day, from, to);
	}
	
	private void memoryCrossIndex(){
		for(Journey j:journeys){
			this.journeysMap.put(j.getId(), j);
		}
	}
}
